package org.common;

import java.util.Calendar;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;

public class DateUtil {

	
	/**
	 * 得到当前服务器时间
	 * @return
	 */
	public static LocalDateTime  getLocalDateTime(){
		return LocalDateTime.now();		
	}
	
	/**
	 * 得到当前时间
	 * @return
	 */
	public static DateTime  getDateTime(){
		return new DateTime();		
	}
	
	/**
	 * 得到当前时间的Millis值
	 * @return
	 */
	public static long getDateTimeMillis(){
		return new DateTime().getMillis();
	}
	
	/**
	 * 得到某一格式日期的Millis值
	 * @param date 日期字符串
	 * @param pattern 日期格式
	 * @return
	 */
	public static long getMillisByString(String date,String pattern){
		return getDateTimeForPattern(date,pattern).getMillis();
	}
	
	/**
	 * 得到当前时区
	 * @return
	 */
	public static DateTimeZone getDateTimeZone(){		
		return new DateTime().getZone();
	}
	

	/**
	 * 得到某一地区的时间 列如："Europe/London"或者"Asia/Shanghai"
	 * @param address
	 * @return
	 */
	public static DateTime  getDateTimeByAddress(String address){
		return DateTime.now(DateTimeZone.forID(address));
	}
	
	/**
	 *  根据指定格式,将时间字符串转换成DateTime对象,这里的格式和上面的输出格式是一样的
	 * @param date    时间字符串
	 * @param pattern 指定格式 列如：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static DateTime getDateTimeForPattern(String date,String pattern){
		return DateTimeFormat.forPattern(pattern).parseDateTime(date);
	}
	
	/**
	 * 将字符串转为datetime 
	 * @param date  字符串列如："2014-01-03"
	 * @return
	 */
	public static DateTime getDateTimeByString (String date){
		return DateTime.parse(date);
	}
	
	/**
	 *  将datetime转为字符串
	 * @param date  字符串列如："2014-01-03"
	 * @return
	 */
	public static String getDateTimeByString (DateTime dateTime,String date){
		return dateTime.toString(date);
	}
	
	/**
	 * 计算两个日期间隔的天数 
	 * @param year1
	 * @param month1
	 * @param day1
	 * @param year2
	 * @param month2
	 * @param day2
	 * @return
	 */
	public static int getDaysBetween(int year1,int month1, int day1, int year2, int month2, int day2){
		LocalDate start=new LocalDate(year1, month1,day1);    
        LocalDate end=new LocalDate(year2, month2, day2);    
        return  Days.daysBetween(start, end).getDays();
	}
	
	/**
	 * 计算两个日期间隔的天数 
	 * @param datetime1
	 * @param datetime2
	 * @return 
	 */
	public static int getDaysBetween(long datetime1,long datetime2){
		LocalDate start=new LocalDate(datetime1);    
        LocalDate end=new LocalDate(datetime2);    
        return  Days.daysBetween(start, end).getDays();
	}
	
	/**
	 * 将日期格式datetime加上相关的年，月，日，时，分，秒，星期
	 * @param datetime
	 * @param years
	 * @param months
	 * @param days
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @param weeks
	 * @return
	 */
	public static DateTime setStringDateTimePlus(String datetime,Integer years,Integer months,Integer days,Integer hours,Integer minutes,Integer seconds,Integer weeks){
		return setDateTimePlus(DateTime.parse(datetime), years, months, days, hours, minutes, seconds, weeks); 
		
	}
	
	/**
	 * 将datetime加上相关的年，月，日，时，分，秒，星期
	 * @param datetime
	 * @param years
	 * @param months
	 * @param days
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @param weeks
	 * @return
	 */
	public static DateTime setDateTimePlus(DateTime datetime,Integer years,Integer months,Integer days,Integer hours,Integer minutes,Integer seconds,Integer weeks){
		if (null!=years){
			setDateTimePlusYears(datetime, years);
		}
		if (null!=months){
			setDateTimePlusMonths(datetime, months);
		}
		if (null!=days){
			setDateTimePlusDays(datetime, days);
		}
		if (null!=hours){
			setDateTimePlusHours(datetime, hours);
		}
		if (null!=minutes){
			setDateTimePlusMinutes(datetime, minutes);
		}
		if (null!=seconds){
			setDateTimePlusSeconds(datetime, seconds);
		}
		if (null!=weeks){
			setDateTimePlusWeeks(datetime, weeks);
		}
		
		return datetime;
	}
	
	/**
	 * 将某一datetime的年相加years
	 * @param datetime
	 * @param years
	 * @return
	 */
	public static DateTime setDateTimePlusYears(DateTime datetime,int years){
		return datetime.plusYears(years);
	}
	
	/**
	 * 将某一datetime的月相加months
	 * @param datetime
	 * @param months
	 * @return
	 */
	public static DateTime setDateTimePlusMonths(DateTime datetime,int months){
		return datetime.plusMonths(months);
	}
	
	/**
	 * 将某一datetime的日相加days
	 * @param datetime
	 * @param days
	 * @return
	 */
	public static DateTime setDateTimePlusDays(DateTime datetime,int days){
		return datetime.plusDays(days);
	}
	
	/**
	 * 将某一datetime的小时相加hours
	 * @param datetime
	 * @param hours
	 * @return
	 */
	public static DateTime setDateTimePlusHours(DateTime datetime,int hours){
		return datetime.plusHours(hours);
	}
	
	/**
	 * 将某一datetime的分钟相加minutes
	 * @param datetime
	 * @param minutes
	 * @return
	 */
	public static DateTime setDateTimePlusMinutes(DateTime datetime,int minutes){
		return datetime.plusMinutes(minutes);
	}
	
	/**
	 * 将某一datetime的秒相加seconds
	 * @param datetime
	 * @param seconds
	 * @return
	 */
	public static DateTime setDateTimePlusSeconds(DateTime datetime,int seconds){
		return datetime.plusSeconds(seconds);
	}
	
	/**
	 * 将某一datetime的星期相加Weeks
	 * @param datetime
	 * @param weeks
	 * @return
	 */
	public static DateTime setDateTimePlusWeeks(DateTime datetime,int weeks){
		return datetime.plusWeeks(weeks);
	}
	
	/**
	 * 将某一datetime的年相减years
	 * @param datetime
	 * @param years
	 * @return
	 */
	public static DateTime setDateTimeMinusYears(DateTime datetime,int years){
		return datetime.minusYears(years);
	}
	
	/**
	 * 将某一datetime的月相减months
	 * @param datetime
	 * @param months
	 * @return
	 */
	public static DateTime setDateTimeMinusMonths(DateTime datetime,int months){
		return datetime.minusMonths(months);
	}
	
	/**
	 * 将某一datetime的日相减days
	 * @param datetime
	 * @param days
	 * @return
	 */
	public static DateTime setDateTimeMinusDays(DateTime datetime,int days){
		return datetime.minusDays(days);
	}
	
	/**
	 * 将某一datetime的小时相减hours
	 * @param datetime
	 * @param hours
	 * @return
	 */
	public static DateTime setDateTimeMinusHours(DateTime datetime,int hours){
		return datetime.minusHours(hours);
	}
	
	/**
	 * 将某一datetime的分钟相减minutes
	 * @param datetime
	 * @param minutes
	 * @return
	 */
	public static DateTime setDateTimeMinusMinutes(DateTime datetime,int minutes){
		return datetime.minusMinutes(minutes);
	}
	
	/**
	 * 将某一datetime的秒相减seconds
	 * @param datetime
	 * @param seconds
	 * @return
	 */
	public static DateTime setDateTimeMinusSeconds(DateTime datetime,int seconds){
		return datetime.minusSeconds(seconds);
	}
	
	/**
	 * 将某一datetime的星期相减Weeks
	 * @param datetime
	 * @param weeks
	 * @return
	 */
	public static DateTime setDateTimeMinusWeeks(DateTime datetime,int weeks){
		return datetime.minusWeeks(weeks);
	}
	
	/**
	 * 将datetime减去相关的年，月，日，时，分，秒，星期
	 * @param datetime
	 * @param years
	 * @param months
	 * @param days
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @param weeks
	 * @return
	 */
	public static DateTime setDateTimeMinus(DateTime datetime,Integer years,Integer months,Integer days,Integer hours,Integer minutes,Integer seconds,Integer weeks){
		if (null!=years){
			setDateTimeMinusYears(datetime, years);
		}
		if (null!=months){
			setDateTimeMinusMonths(datetime, months);
		}
		if (null!=days){
			setDateTimeMinusDays(datetime, days);
		}
		if (null!=hours){
			setDateTimeMinusHours(datetime, hours);
		}
		if (null!=minutes){
			setDateTimeMinusMinutes(datetime, minutes);
		}
		if (null!=seconds){
			setDateTimeMinusSeconds(datetime, seconds);
		}
		if (null!=weeks){
			setDateTimeMinusWeeks(datetime, weeks);
		}
		
		return datetime;
	}
	
	/**
	 * 将日期格式datetime减去相关的年，月，日，时，分，秒，星期
	 * @param datetime
	 * @param years
	 * @param months
	 * @param days
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @param weeks
	 * @return
	 */
	public static DateTime setStringDateTimeMinus(String datetime,Integer years,Integer months,Integer days,Integer hours,Integer minutes,Integer seconds,Integer weeks){
		return setDateTimeMinus(DateTime.parse(datetime), years, months, days, hours, minutes, seconds, weeks); 
		
	}
	
	/**
	 * 查看datetime是否是闰月
	 * @param datetime
	 * @return
	 */
	public static boolean isLeap(DateTime datetime){
		return datetime.monthOfYear().isLeap();
	}
	
	/**
	 * 查看datetime是否是闰月
	 * @param datetime
	 * @return
	 */
	public static boolean isLeapByString(String datetime){
		return DateTime.parse(datetime).monthOfYear().isLeap();
	}
	
	/**
	 * 将java.util.date 转为DateTime
	 * @param date
	 * @return
	 */
	public static DateTime getDatTimeByDate(Date date){
		return new DateTime(date);
	}
	
	/**
	 * 将java.util.calendar 转为DateTime
	 * @param date
	 * @return
	 */
	public static DateTime getDatTimeByDate(Calendar calendar){
		return new DateTime(calendar);
	}
	
	/**
	 * 将DateTime 转为java.util.date
	 * @param date
	 * @return
	 */
	public static Date getDateByDateTime(DateTime dateTime){
		return dateTime.toDate();
	}
	
	/**
	 * 将DateTime 转为java.util.calendar
	 * @param date
	 * @return
	 
	public static Date getCalendarByDateTime(DateTime dateTime){
	}
	*/

}
